package com.bawei.yuekao;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.widget.RemoteViews;

import com.bawei.yuekao.entity.NetSong;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.RequiresApi;

public class MusicService extends Service {
    private MediaPlayer mMediaPlayer = new MediaPlayer();
    private List<NetSong.DataBean> mList = new ArrayList<>(); //数据源
    private Mode mode = Mode.ORDER;
    private int currentPosition;//正在播放歌曲的下标


    public MusicService() {
    }
    //前台服务：自定义通知
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //自定义view
        RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.noti);
        //点击事件：发送广播
        {
            Intent intent1 = new Intent();
            intent1.setAction("com.bawei.music.next");
            remoteViews.setOnClickPendingIntent(R.id.iv_next, PendingIntent.getBroadcast(this,101,intent1,PendingIntent.FLAG_UPDATE_CURRENT));
        }
        {
            Intent intent1 = new Intent();
            intent1.setAction("com.bawei.music.pre");
            remoteViews.setOnClickPendingIntent(R.id.iv_pre, PendingIntent.getBroadcast(this,101,intent1,PendingIntent.FLAG_UPDATE_CURRENT));
        }
        {
            Intent intent1 = new Intent();
            intent1.setAction("com.bawei.music.startOrPause");
            remoteViews.setOnClickPendingIntent(R.id.iv_play, PendingIntent.getBroadcast(this,101,intent1,PendingIntent.FLAG_UPDATE_CURRENT));
        }
        //通知对象
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标，千万不要省略
        builder.setCustomContentView(remoteViews);//自定义通知布局
        Notification notification = builder.build();
        //前台服务
        startForeground(1,notification);


        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MusicBinder();
    }
    public class MusicBinder extends Binder {
        //设置数据资源
        public void setSongs(List<NetSong.DataBean> list){
            mList.clear();
            mList.addAll(list);
        }

        /**
         * 播放指定下标的歌曲
         * @param position
         */
        public void playByPosition(int position){
            currentPosition = position;
            play();
        }

        /**
         * 返回当前正在播放的歌曲
         */
        public NetSong.DataBean getCurrentMusic(){
            if(mList.size() > 0){
                return mList.get(currentPosition);
            }
            return null;
        }


        //切歌
        public void play(){
            if(mMediaPlayer == null){
                mMediaPlayer = new MediaPlayer();
            }
            mMediaPlayer.reset();//注意：重置资源
            try {
                mMediaPlayer.setDataSource(mList.get(currentPosition).getData());
                mMediaPlayer.prepareAsync();
                mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mMediaPlayer.start();
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //播放和暂停
        public void startOrPause(){
            if(mMediaPlayer != null){
                if(mMediaPlayer.isPlaying()){
                    mMediaPlayer.pause();
                }else{
                    mMediaPlayer.start();
                }
            }
        }
        //上一首
        public void pre(){
            if(mode == Mode.ORDER){//顺序
                if(currentPosition == 0){//第一首
                    currentPosition = mList.size()-1;
                }else{
                    currentPosition--;
                }
            }else if(mode == Mode.RANDOM){
                currentPosition = (int) (Math.random()*mList.size());
            }
            play();
        }
        //下一首
        public void next(){
            if(mode == Mode.ORDER){//顺序
                if(currentPosition == mList.size()-1){//最后一首
                    currentPosition = 0;
                }else{
                    currentPosition++;
                }
            }else if(mode == Mode.RANDOM){
                currentPosition = (int) (Math.random()*mList.size());
            }
            play();
        }
        //快进
        public void quick(){
            if(mMediaPlayer != null){
                int currentPosition = mMediaPlayer.getCurrentPosition();
                currentPosition+=5000;
                mMediaPlayer.seekTo(currentPosition);
            }
        }
        //快退
        public void back(){
            if(mMediaPlayer != null){
                int currentPosition = mMediaPlayer.getCurrentPosition();
                currentPosition-=5000;
                mMediaPlayer.seekTo(currentPosition);
            }
        }
        //获得当前进度时间 00:00
        public String getCurrentStr(){
            if(mMediaPlayer != null){
                int currentPosition = mMediaPlayer.getCurrentPosition();//毫秒值
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
                return simpleDateFormat.format(currentPosition);

            }
            return "00:00";
        }
        //获得总时间 00:00
        public String getDurationStr(){
            if(mMediaPlayer != null){
                int currentPosition = mMediaPlayer.getDuration();//毫秒值
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
                return simpleDateFormat.format(currentPosition);

            }
            return "00:00";
        }
        //获得当前进度百分比 0-100    当前进度*100/总时间
        public int getProgress(){
            if(mMediaPlayer != null && mMediaPlayer.getDuration() != 0){
              return mMediaPlayer.getCurrentPosition()*100/mMediaPlayer.getDuration();

            }
            return 0;
        }
        //设置音乐进度 0-100
        public void seekTo(int progress){
            if(mMediaPlayer != null && mMediaPlayer.getDuration() != 0){
                int i = progress * mMediaPlayer.getDuration() / 100;
                mMediaPlayer.seekTo(i);

            }
        }
        //释放媒体资源
        public void release(){
            if(mMediaPlayer != null){
                mMediaPlayer.release();
                mMediaPlayer = null;
            }
        }
        //播放模式切换
        public void setMode(Mode mode_){
            mode = mode_;
        }

    }

}